<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;

class TestController extends AdminController {
	protected $AppSecret = '703099ec53a38802d20e518efb81d4e3';
	protected $AppID = 'wx57b559484ac7a0c1';
	public function _initialize(){
        parent::_initialize();
    }
	
    
//     private function checkSignature()
//     {
//     	$_GET["signature"];
//     	$_GET["timestamp"];
//     	$_GET["nonce"];
    
//     	$tmpArr = array(timestamp, tmpArr, SORT_STRING);
//     	$tmpArr );
//     	$tmpStr );
    
//     	if( signature ){
//     		return true;
//     	}else{
//     		return false;
//     	}
//     }
    
    public function xx(){
  
    		$this->responseMsg();
    	$url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST";
    
   
    $str="<xml><ToUserName><![CDATA[toUser]]></ToUserName>
    	<FromUserName><![CDATA[FromUser]]></FromUserName>
    	<CreateTime>123456789</CreateTime>
    	<MsgType><![CDATA[event]]></MsgType>
    	<Event><![CDATA[subscribe]]></Event>
    	<EventKey><![CDATA[qrscene_123123]]></EventKey>
    	<Ticket><![CDATA[TICKET]]></Ticket>
    	</xml>";
    }
    //验证签名
    public function valid()
    {
    	$echoStr = $_GET["echostr"];
    	$signature = $_GET["signature"];
    	$timestamp = $_GET["timestamp"];
    	$nonce = $_GET["nonce"];
    	$token = 'ddaass';
    	$tmpArr = array($token, $timestamp, $nonce);
    	sort($tmpArr, SORT_STRING);
    	$tmpStr = implode($tmpArr);
    	dump($tmpStr);
    	$tmpStr = sha1($tmpStr);
    	if($tmpStr == $signature){
    		echo $echoStr;
    		exit;
    	}
    }
    

    //响应消息
    public function responseMsg()
    {
    	$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    	if (!empty($postStr)){
    		$this->logger("R \r\n".$postStr);
    		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
    		$RX_TYPE = trim($postObj->MsgType);
    
    		if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")){
    			//过滤关注和取消关注事件
    			/*关注事件应添加自动回复图片消息*/
    
    
    		}else{
    
    		}
    		//若是没有登记进行提示登记
    		if('text' == $RX_TYPE || 'image' == $RX_TYPE ){
    
    
    		}
    		//消息类型分离
    		switch ($RX_TYPE)
    		{
    			case "event":
    				$result = $this->receiveEvent($postObj);
    				break;
    			case "text":
    				if (strstr($postObj->Content, "第三方")){
    					//$result = $this->relayPart3("http://www.fangbei.org/test.php".'?'.$_SERVER['QUERY_STRING'], $postStr);
    				}else{
    					$result = $this->receiveText($postObj);
    				}
    				break;
    			case "image":
    				$result = $this->receiveImage($postObj);
    				break;
    			case "location":
    				$result = $this->receiveLocation($postObj);
    				break;
    			case "voice":
    				$result = $this->receiveVoice($postObj);
    				break;
    			case "video":
    				$result = $this->receiveVideo($postObj);
    				break;
    			case "link":
    				$result = $this->receiveLink($postObj);
    				break;
    			default:
    				$result = "unknown msg type: ".$RX_TYPE;
    				break;
    		}
    		$this->logger("T \r\n".$result);
    		echo $result;
    	}else {
    		echo "";
    		exit;
    	}
    }
}
	
